package com.jungle.app.jw.service.impl;

import com.jungle.common.utils.sql.StringUtils;
import com.jungle.common.core.page.TableDataInfo;
import com.jungle.common.core.domain.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.jungle.app.jw.domain.bo.JwSpotTransportBo;
import com.jungle.app.jw.domain.vo.JwSpotTransportVo;
import com.jungle.app.domain.entity.JwSpotTransport;
import com.jungle.app.jw.service.IJwSpotTransportService;
import com.jungle.app.jw.repository.JwSpotTransportRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 景点交通Service业务层处理
 *
 * @author jungle
 * @date 2023-12-04
 */
@RequiredArgsConstructor
@Service
public class JwSpotTransportServiceImpl implements IJwSpotTransportService {

    private final JwSpotTransportRepository repository;

    /**
     * 查询景点交通
     */
    @Override
    public JwSpotTransportVo queryById(String id){
        return repository.findById(id).map(v -> com.jungle.common.utils.BeanCopyUtils.copy(v, JwSpotTransportVo.class)).orElse(null);
    }

    /**
     * 查询景点交通列表
     */
    @Override
    public TableDataInfo<JwSpotTransportVo> queryPageList(JwSpotTransportBo bo, PageQuery pageQuery) {
        Page<JwSpotTransport> result = repository.findAll(getSpecification(bo), pageQuery.pageable());
        return TableDataInfo.build(result, JwSpotTransportVo.class);
    }

    /**
     * 查询景点交通列表
     */
    @Override
    public List<JwSpotTransportVo> queryList(JwSpotTransportBo bo) {
        return repository.findAll(getSpecification(bo)).stream()
            .map(v -> com.jungle.common.utils.BeanCopyUtils.copy(v, JwSpotTransportVo.class))
            .collect(Collectors.toList());
    }

    private Specification<JwSpotTransport> getSpecification(JwSpotTransportBo bo) {
        return (root, query, cb) -> {
            Map<String, Object> params = bo.getParams();
            List<Predicate> predicates = new ArrayList<>();
            if (bo.getCreateBy() != null) {
                    predicates.add(cb.equal(root.get("createBy"), bo.getCreateBy()));
            }
            if (bo.getCreateTime() != null) {
                    predicates.add(cb.equal(root.get("createTime"), bo.getCreateTime()));
            }
            if (bo.getUpdateBy() != null) {
                    predicates.add(cb.equal(root.get("updateBy"), bo.getUpdateBy()));
            }
            if (bo.getUpdateTime() != null) {
                    predicates.add(cb.equal(root.get("updateTime"), bo.getUpdateTime()));
            }
            if (StringUtils.isNotBlank(bo.getCost())) {
                    predicates.add(cb.equal(root.get("Cost"), bo.getCost()));
            }
            if (StringUtils.isNotBlank(bo.getDeparture())) {
                    predicates.add(cb.equal(root.get("Departure"), bo.getDeparture()));
            }
            if (StringUtils.isNotBlank(bo.getDestination())) {
                    predicates.add(cb.equal(root.get("Destination"), bo.getDestination()));
            }
            if (StringUtils.isNotBlank(bo.getDetails())) {
                    predicates.add(cb.equal(root.get("Details"), bo.getDetails()));
            }
            if (StringUtils.isNotBlank(bo.getDistance())) {
                    predicates.add(cb.equal(root.get("Distance"), bo.getDistance()));
            }
            if (StringUtils.isNotBlank(bo.getEstimatedTime())) {
                    predicates.add(cb.equal(root.get("EstimatedTime"), bo.getEstimatedTime()));
            }
            if (StringUtils.isNotBlank(bo.getScenicSpotID())) {
                    predicates.add(cb.equal(root.get("ScenicSpotID"), bo.getScenicSpotID()));
            }
            if (StringUtils.isNotBlank(bo.getTransportationID())) {
                    predicates.add(cb.equal(root.get("TransportationID"), bo.getTransportationID()));
            }
            if (StringUtils.isNotBlank(bo.getTransportationMode())) {
                    predicates.add(cb.equal(root.get("TransportationMode"), bo.getTransportationMode()));
            }
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        };
    }

    /**
     * 新增景点交通
     */
    @Override
    public Boolean insertByBo(JwSpotTransportBo bo) {
        JwSpotTransport add = com.jungle.common.utils.BeanCopyUtils.copy(bo, JwSpotTransport.class);
        validEntityBeforeSave(add);
        repository.saveAndFlush(add);
        bo.setId(add.getId());
        return true;
    }

    /**
     * 修改景点交通
     */
    @Override
    public Boolean updateByBo(JwSpotTransportBo bo) {
        JwSpotTransport update = com.jungle.common.utils.BeanCopyUtils.copy(bo, JwSpotTransport.class);
        validEntityBeforeSave(update);
        return repository.save(update) != null;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JwSpotTransport entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除景点交通
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        repository.deleteAllById(ids);
        return true;
    }

}
